//
//  MyAlertOptionViewV3.m
//  BaseLib
//
//  Created by CPX on 2018/9/14.
//

#import "MyAlertOptionViewV3.h"
#import "YYCategories.h"
#import "Global.h"
#import "BaseTBCell.h"
#import "Masonry.h"

/**
 普通弹框
 @author yzl
 */
@interface MyAlertOptionCellV3:BaseTBCell

@property (nonatomic,strong) UILabel *titleLab;
//@property (nonatomic,strong) UIImageView *accessoryImage;
@property (nonatomic) BOOL tbSelected;

@end

@implementation MyAlertOptionCellV3

- (UILabel*)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _titleLab.font = [UIFont systemFontOfSize:16];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.textColor = UIColorHex(001208);
        [self.contentView addSubview:_titleLab];
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(20);
            make.leading.mas_equalTo(16);
            make.trailing.mas_equalTo(-16);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    return _titleLab;
}

//- (UIImageView*)accessoryImage{
//    if (!_accessoryImage) {
//        _accessoryImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select_ic_green"]];
//        [self.contentView addSubview:_accessoryImage];
//        [_accessoryImage mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(15, 15));
//            make.trailing.mas_equalTo(-16);
//            make.centerY.mas_equalTo(self.contentView);
//        }];
//    }
//    return _accessoryImage;
//}

- (void)setTbSelected:(BOOL)tbSelected{
    _tbSelected = tbSelected;
    if (!tbSelected) {
//        self.accessoryImage.hidden= YES;
        self.titleLab.textColor = UIColorHex(#334139);
    }
    else{
//        self.accessoryImage.hidden = NO;
        self.titleLab.textColor = UIColorHex(#00732F);
    }
}

@end

@interface MyAlertOptionViewV3()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSArray *options;
@property (nonatomic,strong) UITableView *table;
@property (nonatomic,strong) UIView *whiteView;

@property (nonatomic, strong) UIImageView *leftIV;
@property (nonatomic, strong) UIImageView *rightIV;
@property (nonatomic, strong) UIImageView *middelIV;

@property (nonatomic,strong) NSMutableArray *btns;
@property (nonatomic) NSInteger selectedIdx;

@end

@implementation MyAlertOptionViewV3

+ (instancetype)viewWithOptions:(NSArray *)options selectIdx:(NSInteger)idx{
    MyAlertOptionViewV3 *view = [MyAlertOptionViewV3 new];
    view.removeOnTouchOutside = NO;
    view.selectedIdx = idx;
    view.options = options;
    return view;
}

- (void)initUI{
    [super initUI];
    
    self.whiteView = [UIView new];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    self.whiteView.layer.cornerRadius = 5;
    self.whiteView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.whiteView];
    self.table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.dataSource = self;
    
    [self.whiteView addSubview:self.table];
    
    self.leftIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pop_left_img"]];
    [self.whiteView addSubview:self.leftIV];
    
    self.middelIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pop_middle_img"]];
    [self.whiteView addSubview:self.middelIV];
    
}

- (void)addActionWithTitle:(NSString *)title color:(UIColor *)color{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [self.whiteView addSubview:btn];
    btn.layer.borderColor = UIColorHex(#EFEFEF).CGColor;
    btn.layer.borderWidth = .5f;
    [btn addTarget:self action:@selector(onButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    [self.btns addObject:btn];
    
    [self setNeedsLayout];
}

- (NSMutableArray*)btns{
    if(!_btns){
        _btns = [NSMutableArray arrayWithCapacity:4];
    }
    return _btns;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat h = 52;
    CGFloat w = self.whiteView.width/(self.btns.count?self.btns.count:1);
    //    CGFloat right = self.titleLable.superview.width - 8;
    CGFloat bottom = self.whiteView.height;
    CGFloat t1 = 0;
    CGFloat t2 = self.whiteView.width;
    for (NSInteger i = self.btns.count -1; i>=0; i--) {
        UIButton *btn = self.btns[i];
        btn.size = CGSizeMake(w, h);
        
        btn.bottom = bottom;
        if ([UIView appearance].semanticContentAttribute == UIWebPaginationModeRightToLeft) {
            btn.left = t1;
            t1+=w;
        }
        else{
            
            
            btn.right = t2;
            t2-= w;
        }
    }
}

- (void)setOptions:(NSArray *)options{
    _options = options;
    CGFloat height = [Global cellHeightForStandard:50]*options.count+40;
    CGFloat minHeight = 40+80;
    self.whiteView.width = kScreenWidth - 40*2;
    self.whiteView.height = height+minHeight;
    self.whiteView.center = self.contentView.center;
    
    self.table.height = height;
    self.table.width = self.whiteView.width;
    self.table.left = 0;
    self.table.top = 10+40;
    [self.table reloadData];
    
    self.rightIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pop_right_img"]];
    [self.whiteView addSubview:self.rightIV];
    self.leftIV.left = 0;
    self.rightIV.right = self.whiteView.width;
    self.middelIV.width = self.whiteView.width - self.leftIV.width-self.rightIV.width;
    self.middelIV.left = self.leftIV.right;
}

#pragma mark - table

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyAlertOptionCellV3 *cell = [MyAlertOptionCellV3 cellWithTable:tableView];
    cell.titleLab.text = self.options[indexPath.row];
    cell.tbSelected = indexPath.row == self.selectedIdx;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.options.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [Global cellHeightForStandard:50];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectedIdx = indexPath.row;
    [self.table reloadData];
}

- (void)onButtonTouched:(UIButton*)sender{
    NSInteger idx = [self.btns indexOfObject:sender];
    if (self.block) {
        self.block(idx,self.selectedIdx);
    }
    [self removeFromSuperview];
}

@end
